package com.itheima.health.common;

import com.itheima.health.service.SetMealService;
import com.itheima.health.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

@Component
public class AliyunOssScanClean {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private QiniuUtils qiniuUtils;
//0 0 02 * * ?
    @Scheduled(cron = "0 0 02 * * ?")
    public void run() {
        HashOperations ops = redisTemplate.opsForHash();
        Set qiniuyun = ops.keys("qiniuyun");
        for (Object o : qiniuyun) {
            String name = (String) o;
            long time = (long) ops.get("qiniuyun", name);
            long timeNow = new Date().getTime();
            if (timeNow - time >1000*60*10) {
                if(setMealService.checkImg(name)){
                    ops.delete("qiniuyun",o);
                    qiniuUtils.deleteFileFromQiniu(name);
                }
            }
        }

    }

}
